﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToolwebAQ.database;
using System.Data.Linq;
using Ultility;
using System.Data;
using System.Data.SqlClient;

namespace ToolwebAQ
{
    public partial class AQPagePermissionAccount : System.Web.UI.Page
    {
        tool helper = new tool();
        aquariusDBDataContext db = new aquariusDBDataContext();
        public int stt = 1;
        protected void Page_Load(object sender, EventArgs e)
        {

            helper.SetMetaData(this, "Phân  trang cho tài khoản", "", "keywords");
            helper.SetMetaData(this, "Phân  trang cho tài khoản", "", "Description");
            if (Session[Source.obj_Account] != null)
            {
                if (Session[Source.obj_role].ToString() == "True")
                {

                    if (!IsPostBack)
                    {

                    }

                }
                else
                {
                    Response.Redirect("404.html");
                }
            }
            else
            {
                Response.Redirect("../adminaq/AQLogin.aspx");
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            BindPermissionPage();
            BindPermissionOnAccount();
            BindAccount();
        }
        private void BindPermissionPage()
        {

            var allmenu = from menu in db.Tbl_pagemenus
                          from cate in db.Tbl_categorymenus
                          where menu.idcatemenu == cate.idcatemenu
                          select new
                          {
                              idmenupage = menu.idmenupage,
                              Namepage = menu.Namepage,
                              Urlpage = menu.Urlpage,
                              Isused = menu.Isused,
                              Namemenu = cate.Namemenu
                          };
            SqlCommand cmd = (SqlCommand)db.GetCommand(allmenu);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            gvPermissionpage.DataSource = ds;
            gvPermissionpage.DataBind();
        }

        private void BindPermissionOnAccount()
        {
            var listPage = from menu in db.Tbl_pagemenus
                           from cate in db.Tbl_categorymenus
                           from acc in db.Tbl_Accountpages
                           where menu.idcatemenu == cate.idcatemenu && menu.idmenupage == acc.idmenupage
                           select new
                           {
                               Idacpage = acc.Idacpage,
                               Accountname = acc.Accountname,
                               Namepage = menu.Namepage,
                               Urlpage = menu.Urlpage,
                               Namemenu = cate.Namemenu,
                               update=acc.Update,
                               insert=acc.Insert,
                               delete=acc.Delete,
                               print=acc.print,
                               search=acc.search
                           };
            SqlCommand cmd = (SqlCommand)db.GetCommand(listPage);
            DataSet dsAcc = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dsAcc);
            gvPermissionAccount.DataSource = dsAcc;
            gvPermissionAccount.DataBind();

        }

        private void BindAccount()
        {
            //DropDownListaccount.DataSource = db.Sp_Tbl_Account_select_dstaikhoanadmin().ToList();
            //DropDownListaccount.DataTextField = "Accountname";
            //DropDownListaccount.DataValueField = "Accountname";
            //DropDownListaccount.DataBind();
            //DropDownListaccount.Items.Insert(0, new ListItem("Chọn tài khoản ", "0"));
        }

        protected void gvPermissionAccount_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            BindPermissionOnAccount();
        }

        protected void gvPermissionpage_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            BindPermissionPage();
        }
    }
}